﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using kim.Common;
using System.Xml;


namespace kim.Web.rpp0717.Xmloperating
{
    public partial class AddOrEdit : kim.UI.ManagePage
    {
        protected string type,strPath;
        private int Nid,typeid;
        XmlDocument xmlDoc;
        protected void Page_Load(object sender, EventArgs e)
        {
            typeid = kim.Common.KIMRequest.GetInt("typeid", 1);
            strPath = "~/flash" + typeid + "/tagcloud.xml";
            Nid = KIMRequest.GetInt("id", 1);
            type = KIMRequest.GetString("type");
            if (!IsPostBack)
                GetRequestValues();

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                if (DoEdit(Nid.ToString()))
                    lbmsg.Text = "修改成功";
                else
                    lbmsg.Text = "修改失败";
            }
            if (type == KIMEnums.ActionEnum.Add.ToString())
            {
                if (DoAdd())
                    lbmsg.Text = "添加成功";
                else
                    lbmsg.Text = "添加失败";
            }

        }

        #region 编辑添加基本操作=========================================
        private bool DoEdit(string id)
        {
            Model.tagXml mo = new Model.tagXml();
            mo.Id =id;
            mo.Value = this.txtTitle.Value;
            mo.Style = "font-size: 20pt;";
            mo.Href = this.txtUrl.Value;
            LoadXml();
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("tags").ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("id") == mo.Id)
                {
                    xe.SetAttribute("href", mo.Href);
                    xe.SetAttribute("style", mo.Style);
                    xe.InnerText = mo.Value;
                    break;
                }
            }
            string xmlPath = Server.MapPath(strPath);
            xmlDoc.Save(xmlPath);
            return true;
        }
        private bool DoAdd()
        {
            Model.tagXml mo = new Model.tagXml();
            mo.Id = (GetMaxId() + 1).ToString();
            mo.Value = this.txtTitle.Value;
            mo.Style = "font-size: 20pt;";
            mo.Href = this.txtUrl.Value;
            LoadXml();
            XmlNode xmldocSelect = xmlDoc.SelectSingleNode("tags");
            XmlElement el = xmlDoc.CreateElement("a");//添加A节点
            el.SetAttribute("id", mo.Id);
            el.SetAttribute("href", mo.Href);
            el.SetAttribute("style", mo.Style);
            el.InnerText = mo.Value;
            xmldocSelect.AppendChild(el);
            string xmlPath = Server.MapPath(strPath);
            xmlDoc.Save(xmlPath);
            return true;
        }
        #endregion

        private void GetRequestValues()
        {

            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                GetEditValue();
            }
            if (type == KIMEnums.ActionEnum.Delete.ToString())
            {
                bool del = DelNode();
                Response.Redirect("List.aspx?typeid="+this.typeid);
            }
            if (type != KIMEnums.ActionEnum.Edit.ToString() && type != KIMEnums.ActionEnum.Add.ToString() && type != KIMEnums.ActionEnum.Delete.ToString())
                Response.Redirect("../indexpage.aspx");
        }
        private void GetEditValue()
        {
            Model.tagXml mo = GetTagXml();
            this.txtTitle.Value = mo.Value;
            this.txtUrl.Value = mo.Href;
        }

        private void LoadXml()
        {
            xmlDoc = new XmlDocument();
            string xmlPath = Server.MapPath(strPath);
            xmlDoc.Load(xmlPath);
        }
        private int GetMaxId()
        {
            int maxId = 0;
             LoadXml();
            Model.tagXml mo = new Model.tagXml();
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("tags").ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                XmlElement xe = (XmlElement)xn;
                if (maxId < int.Parse(xe.GetAttribute("id")))
                    maxId = int.Parse(xe.GetAttribute("id"));
            }
            return maxId;
        }

        private Model.tagXml GetTagXml()
        {
            LoadXml();
            Model.tagXml mo = new Model.tagXml();
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("tags").ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("id") == this.Nid.ToString())
                {
                    mo.Id = xe.GetAttribute("id");
                    mo.Style = xe.GetAttribute("style");
                    mo.Href = xe.GetAttribute("href");
                    mo.Value = xe.InnerText;
                }
            }
            return mo;
        }

        private bool DelNode()
        {
            bool del=false;
            LoadXml();
            XmlNodeList xnl = this.xmlDoc.SelectSingleNode("tags").ChildNodes;
            foreach (XmlNode xn in xnl)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("id") == this.Nid.ToString())
                {
                    xe.ParentNode.RemoveChild(xe);
                    del = true;
                    break;
                }
            }
            string xmlPath = Server.MapPath(strPath);
            xmlDoc.Save(xmlPath);
            return del;
        }

    }
}